<template>
	<view class="container-nav">
		<view class="w-nav">
			<view class="nav-title" @click="onClkTitle(0)">
				<text :class="{'nav-focus':active==0}">发起提交</text>
			</view>
			<view class="nav-title" @click="onClkTitle(1)">
				<text :class="{'nav-focus':active==1}">查看数据</text>
			</view>
		</view>
		<view class="nav-body-btn" v-show="active==0">
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>出差事由</view>
					</view>
					<view class="row-later-time">
						<editor placeholder="请输入出差事由" id="r_editor" @input="onReason" @ready="onTextReady"></editor>
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>交通工具</view>
					</view>
					<view class="row-later-time">
						<picker @change="onPickerChange" :value="lt_arr[lt_index].id" :range="lt_arr" :range-key="'name'">
							{{lt_arr[lt_index].name}}
						</picker>
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>目的城市</view>
					</view>
					<view class="row-later-time" @click="onNavTo()">
						{{Destination}}
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>开始时间</view>
					</view>
					<view class="row-later-time" @click="onSelDatePicker(0)">
						{{BeginTime}}
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>结束时间</view>
					</view>
					<view class="row-later-time" @click="onSelDatePicker(1)">
						{{EndTime}}
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>住宿费</view>
					</view>
					<view class="row-later-time">
						<input type="number" class="w-txt" v-model="Hotel" />
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>交通费</view>
					</view>
					<view class="row-later-time">
						<input type="number" class="w-txt" v-model="Carfare" />
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>餐费</view>
					</view>
					<view class="row-later-time">
						<input type="number" class="w-txt" v-model="Food" />
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>其他费</view>
					</view>
					<view class="row-later-time">
						<input type="number" class="w-txt" v-model="OtherFee" />
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-dot">*</view>
						<view>总费用</view>
					</view>
					<view class="row-later-time num">
						{{TotalCost | f_TotalCost}}
					</view>
				</view>
			</view>
			<view class="w-box">
				<view class="w-box-row">
					<view class="row-later-title">
						<view class="later-title-los">*</view>
						<view>费用说明</view>
					</view>
					<view class="row-later-time">
						<editor placeholder="请输入费用详细说明" id="n_editor" @input="onFeeNote" @ready="onNoteReady"></editor>
					</view>
				</view>
			</view>
			<view class="w-btns">
				<button type="primary" :disabled="blnDisabled_w" @click="onAddTravelInfo">{{blnEditStatus?"更新":"提交"}}</button>
			</view>
		</view>
		<view class="nav-body" v-show="active==1">
			<view class="w-box" v-for="(item,index) in items" :key="index" @click.stop="onNavTo(item.ID)">
				<view class="w-box-title">
					<view class="w-box-title-img">{{_retSlgName(item.Traveller)}}</view>
					<view>
						<view class="w-box-title-header">
							{{item.Traveller}}提交的出差
						</view>
						<view class="w-box-title-dt">
							出差事由：{{item.Reason}}
						</view>
						<view class="w-box-title-dt">
							开始时间：{{_retFormatYmdt(item.Startdate)}}
						</view>
						<view class="w-box-title-dt">
							结束时间：{{_retFormatYmdt(item.Enddate)}}
						</view>
						<view class="w-box-title-dt">
							交通工具：{{item.TransportType}}
						</view>
						<view class="w-box-title-act">
							<view class="act-status">
								{{item.FlowStatus_Name}}
							</view>
							<view class="act-edit" v-show="_retBtnsVal(item.ValidBtn,'vj-btn-edit')" @click.stop="onEditTravel(item.ID)">
								修改
							</view>
							<view class="act-cancel" v-show="_retBtnsVal(item.ValidBtn,'vj-btn-del')" @click.stop="onDeleTravel(item.ID)">
								撤销
							</view>
							<view class="pass" v-show="_retStatusVal(item.FlowStatus,0)">
								已完成
							</view>
							<view class="refuse" v-show="_retStatusVal(item.FlowStatus,3)">
								已拒绝
							</view>
						</view>
					</view>
					<view class="w-box-title-org">
						{{_retFormatYmdt(item.Createtime)}}
					</view>
				</view>
			</view>
			<view class="w-box" v-show="items.length==0">
				{{loading}}
			</view>
		</view>
		<date-time-picker ref='date-time' :endYear="endYear" type='date' @change='onDateTimeChange'></date-time-picker>
	</view>
</template>

<script>
	let pageApp = getApp();
	import DateTimePicker from '@/components/uni-dateTimePicker/uni-dateTimePicker.vue'
	import {
		city_list
	} from "@/common/citylist.js"
	export default {
		data() {
			return {
				active: 0,
				Reason: "",
				loading: "数据正在加载中...",
				endYear: 2030,
				lt_arr: JSON.parse(uni.getStorageSync("transporttype")),
				lt_index: 0,
				Destination: "请选择城市",
				Hotel: 0,
				Carfare: 0,
				Food: 0,
				OtherFee: 0,
				BeginTime: "请选择时间",
				_BeginTime: "",
				EndTime: "请选择时间",
				_EndTime: "",
				StartOrEnd: 0,
				items: [],
				FeeNote: "",
				id: -1,
				blnDisabled_w: true,
				blnEditStatus: false
			}
		},
		computed: {
			TotalCost() {
				return parseInt(this.Hotel) + parseInt(this.Carfare) + parseInt(this.Food) + parseInt(this.OtherFee);
			}
		},
		components: {
			DateTimePicker
		},
		filters: {
			f_TotalCost(e) {
				if (Number(e) >= 0) {
					return e;
				} else {
					return "计算中..."
				}
			}
		},
		watch: {
			BeginTime: {
				handler: function(n, o) {
					// console.log(n)
					this._BeginTime = n;
					this.diffDateTime(this._EndTime, this._BeginTime);
				}
			},
			EndTime: {
				handler: function(n, o) {
					this._EndTime = n;
					this.diffDateTime(this._EndTime, this._BeginTime);
				}
			}
		},
		onLoad: function(options) {
			// console.log(options.id);
			if (options.id) {
				this.blnEditStatus = true;
				this.id = options.id;
				this.editTravelInfo(this.id);
			}
		},
		onShow: function() {
			uni.$on("seletedCity", (d) => {
				this.Destination = d.cityName;
			})
			if (this.active) {
				// 加载出差历史
				this.getTravelInfo();
			}
		},
		methods: {
			onNavTo(id) {
				if (id) {
					uni.navigateTo({
						url: "./TravelInfo?id=" + id
					})
				} else {
					uni.navigateTo({
						url: "./TravelCityList"
					})
				}
			},
			onTextReady(e) {
				if (this.blnEditStatus) {
					uni.createSelectorQuery().select('#r_editor').context((res) => {
						this.editorCtx = res.context;
						this.editorCtx.setContents({
							html: this.Reason ? this.Reason : ""
						})
					}).exec()
				}
			},
			onNoteReady(e) {
				if (this.blnEditStatus) {
					uni.createSelectorQuery().select('#n_editor').context((res) => {
						this.editorCtx = res.context;
						this.editorCtx.setContents({
							html: (this.FeeNote != "null") ? this.FeeNote : ""
						})
					}).exec()
				}
			},
			_retFormatDate(v) {
				return v ? v.substr(0, v.length - 3) : "";
			},
			_retSlgName(v) {
				return (v) ? v.substr(0, 1) : "";
			},
			_retFormatYmdt(v) {
				return v ? this.retFormatYmdt(v) : "";
			},
			_retBtnsVal(v, t) {
				return v.indexOf(t) != -1 ? true : false;
			},
			_retDecode(v) {
				return this.Base64.decode(v);
			},
			_retStatusVal(v, t) {
				let _tmp_arr_refuse = [644, 645, 646];
				let _tmp_arr_status = JSON.parse(uni.getStorageSync("travel_status"));
				switch (t) {
					case 0: //完成
						return v == _tmp_arr_status[1].id ? true : false;
						break;
					case 1: //撤销
						return v == _tmp_arr_status[2].id ? true : false;
						break;
					case 3: //拒绝
						return (v == _tmp_arr_refuse[0] || v == _tmp_arr_refuse[1] || v == _tmp_arr_refuse[2]) ? true : false;
						break;
				}
			},
			diffDateTime(e, b) {
				if (e && b) {
					// 两个时间相减
					let _e = new Date(Date.parse(e.replace(/-/g, "/")));
					let _b = new Date(Date.parse(b.replace(/-/g, "/")));
					// 相差值
					let _t = _e - _b;
					if (_t <= 0) {
						uni.showToast({
							icon: "none",
							title: "结束时间必须大于开始时间！",
							success: () => {
								this.blnDisabled_w = true;
							}
						})
					} else {
						this.blnDisabled_w = this.checkIsEmpty(this.Reason, "0000");
					}
				}
			},
			onReason(e) {
				this.Reason = e.detail.text;
				this.blnDisabled_w = this.checkIsEmpty(this.Reason, "0000");
				this.diffDateTime(this._EndTime, this._BeginTime);
			},
			onFeeNote(e) {
				this.FeeNote = e.detail.text;
			},
			onClkTitle(e) {
				this.active = e;
				if (e) {
					// 加载出差历史
					this.getTravelInfo();
				}
			},
			onPickerChange(e) {
				this.lt_index = e.target.value;
			},
			onSelDatePicker(index) {
				this.StartOrEnd = index;
				this.$refs['date-time'].show();
			},
			onDateTimeChange(value) {
				if (this.StartOrEnd) {
					this.EndTime = value;
				} else {
					this.BeginTime = value;
				}
			},
			onAddTravelInfo() {
				if (this.Destination != "请选择城市" && this.BeginTime != "请选择时间" && this.EndTime != "请选择时间") {
					let _url = pageApp.globalData.url + "/TravelNew";
					let _data = {
						"Traveller": JSON.parse(uni.getStorageSync("userLoginData")).UserID,
						"Destination": this.Destination,
						"FlowStatus": this.FlowStatus,
						"Reason": this.Reason,
						"Startdate": this._BeginTime,
						"Enddate": this._EndTime,
						"TransportType": this.lt_index,
						"TotalCost": this.TotalCost,
						"Food": this.Food,
						"Hotel": this.Hotel,
						"Carfare": this.Carfare,
						"OtherFee": this.OtherFee,
						"FeeNote": this.FeeNote,
						"OrgID": JSON.parse(uni.getStorageSync("userLoginData")).OrgID,
						"DepartmentID": JSON.parse(uni.getStorageSync("userLoginData")).OrgID
					}
					if (this.blnEditStatus) {
						_url = pageApp.globalData.url + "/TravelUpdate";
						_data.ID = this.id;
					}
					//准备数据
					let apiData = {
						url: _url,
						data: _data,
						method: "POST",
						success: (d) => {
							if (d.data.code == "001") {
								uni.showToast({
									icon: "success",
									title: "操作成功！"
								})
								setTimeout(() => {
									if (this.blnEditStatus) {
										uni.navigateBack({
											delta: 1
										})
									} else {
										this.onClkTitle(1);
									}

								}, 1000)
							} else {
								uni.showToast({
									icon: "fail",
									title: "操作失败！"
								})
							}
						}
					}
					//发送数据请求
					this.vjAjaxData(apiData);
				} else {
					uni.showToast({
						icon: "none",
						title: "信息填写不完整！"
					})
				}
			},
			getTravelInfo() {
				// 准备数据
				let apiData = {
					url: pageApp.globalData.url + "/TravelList",
					data: {
						"page": 1,
						"rows": 10
					},
					success: (d) => {
						// console.log("数据", d)
						if (d.data.code == "001") {
							this.items = d.data.data.items;
						}
					}
				}
				// 发送数据
				this.vjAjaxData(apiData);
			},
			editTravelInfo(ID) {
				// 准备数据
				let apiData = {
					url: pageApp.globalData.url + "/TravelInfo",
					data: {
						"ID": ID
					},
					success: (d) => {
						// console.log("数据", d.data.data)
						if (d.data.code == "001") {
							this.BeginTime = d.data.data.Startdate;
							this._BeginTime = this.BeginTime;
							this.EndTime = d.data.data.Enddate;
							this._EndTime = this.EndTime;
							this.lt_index = d.data.data.TransportType;
							this.FlowStatus = d.data.data.FlowStatus;
							this.Destination = d.data.data.Destination;
							this.FeeNote = this.Base64.unescape(d.data.data.FeeNote);
							this.Food = d.data.data.Food;
							this.Hotel = d.data.data.Hotel;
							this.OtherFee = d.data.data.OtherFee;
							this.Reason = this.Base64.unescape(d.data.data.Reason);
						}
					}
				}
				// 发送数据
				this.vjAjaxData(apiData);
			},
			onEditTravel(id) {
				uni.navigateTo({
					url: "./TravelNew?id=" + id
				})
			},
			onDeleTravel(id) {
				uni.showModal({
					title: "微简系统",
					content: "真的要撤销申请吗？",
					success: (res) => {
						if (res.confirm) {
							// 准备请求数据
							let apiData = {
								url: pageApp.globalData.url + "/TravelDelete",
								data: {
									"ID": id
								},
								method: "POST",
								success: (e) => {
									uni.showToast({
										title: "撤销成功！"
									})
									this.getTravelInfo();
								}
							}
							// 发送数据请求
							this.vjAjaxData(apiData);
						}
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #F1F5F6;
	}

	.container-nav {
		font-size: 14px;
		line-height: 24px;
	}

	.ql-container {
		height: 100rpx;
		min-height: 100rpx;
	}

	.w-nav {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
		background-color: #FFFFFF;
		padding: 20rpx 0;
	}

	.nav-body-btn {
		padding-bottom: 136rpx;
	}

	.nav-focus {
		border-bottom: solid 6rpx #4497e1;
		padding-bottom: 20rpx;
	}

	.nav-title {
		width: 50%;
		text-align: center;
	}

	.nav-tip {
		position: relative;
		padding: 10rpx;
		display: flex;
		width: 100%;
		top: -30rpx;
		background-color: #4497e1;
		color: #ffffff;
		font-size: 26rpx;
		justify-content: space-around;
	}

	.w-box {
		margin: 20rpx 0;
		padding: 40rpx;
		background-color: #ffffff;
		position: relative;

	}

	.w-box .pass {
		border: solid 6rpx #a7e3cf;
		position: absolute;
		right: 0;
		bottom: 0;
		width: 140rpx;
		height: 140rpx;
		text-align: center;
		line-height: 140rpx;
		border-radius: 50%;
		color: #a7e3cf;
		font-weight: 700;
		transform: rotateZ(-30deg);
	}

	.w-box .refuse {
		border: solid 6rpx #f2bbb7;
		position: absolute;
		right: 0;
		bottom: 0;
		width: 140rpx;
		height: 140rpx;
		text-align: center;
		line-height: 140rpx;
		border-radius: 50%;
		color: #f2bbb7;
		font-weight: 700;
		transform: rotateZ(-30deg);
	}

	.row-later-title {
		display: flex;
		font-size: 34rpx;
	}

	.later-title-dot {
		padding: 8rpx;
		color: red;
	}

	.later-title-los {
		padding: 8rpx;
		color: #ffffff;
	}

	.row-later-time {
		margin-left: 30rpx;
	}

	.w-box-title {
		display: flex;
		position: relative;
	}

	.w-box-title-img {
		width: 60rpx;
		height: 60rpx;
		padding: 4rpx;
		text-align: center;
		line-height: 60rpx;
		border-radius: 16rpx;
		background-color: #4497e1;
		margin-right: 16rpx;
		margin-top: 10rpx;
		color: #ffffff;
		font-size: 34rpx;
	}

	.w-box-title-header,
	.num {
		font-size: 34rpx;
		color: #000000;
		font-weight: 700;
	}

	.w-box-title-dt {
		font-size: 28rpx;
		line-height: 34rpx;
	}

	.w-box-title-act {
		display: flex;
		justify-content: space-between;
	}

	.act-status {
		color: #E1B176
	}

	.act-cancel,
	.act-edit {
		position: absolute;
		color: #4497e1;
	}

	.act-cancel {
		right: 0;
	}

	.act-edit {
		right: 100rpx;
	}

	.w-box-title-org {
		position: absolute;
		margin-top: 16rpx;
		right: 0;
	}

	.w-box-row {
		font-size: 30rpx;
		line-height: 36rpx;
	}

	.w-box-row text {
		overflow: hidden;
		display: inline-block;
		text-overflow: ellipsis;
		white-space: nowrap;
		width: 670rpx;
	}

	.w-btns {
		background-color: #ffffff;
		padding: 20rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}

	.w-btns button {
		width: 710rpx;
		margin-left: 0;
	}

	.w-txt {
		font-size: 30rpx;
	}
</style>
